Overview

Summary:

This code provides two examples of spatial data visualization for oil spill data in California. The exploratory data visualization is in an interactive format, while the choropleth map is a static map in a finalized format. Both highlight how the same data can be shown in different ways to visualize different aspects of the spatial data. For example, the full data set is likely useful for interactive exploration, but would make for a messy map if we were trying to convey counts in an easy to understand format. A choropleth makes the data easy to interpret by obscures the exact location a spill occurred.

Data Citation: CA Department of Fish and Wildlife, Office of Spill Prevention and Response, “Oil spill Incident Tracking [ds394],” https://gis.data.ca.gov/datasets/7464e3d6f4924b50ad06e5a553d71086_0/explore?showTable=true

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

# attach libraries
library(spatstat)
library(tmap)
library(tmaptools)
library(sf)
library(tidyverse)
library(here)

Exploratory Map

Read in the data

# spill data
spills <- read_sf(here('data', 'Oil_Spill_Incident_Tracking_[ds394]', "Oil_Spill_Incident_Tracking_[ds394].shp")) %>% 
  janitor::clean_names()

# county shapefile
ca_counties <- read_sf(here('data', 'ca_counties', 'CA_Counties_TIGER2016.shp')) %>% 
  janitor::clean_names()

Create interactive exploratory map

# interactive mode
tmap_mode('view')

# creation of exploratory map with both layers
tm_shape(ca_counties)+
  tm_fill(col = 'black', alpha = 0.3)+
  tm_borders(col = 'black', lwd = 0.5)+
  tm_shape(spills) +
  tm_dots()

Creation of visualization for 2008 Inland Spills by County

Subset and join data

# subset of data
spills_inland <- spills %>% 
  filter(inlandmari == "Inland") %>% 
  mutate(dateofinci = lubridate::ymd(dateofinci),
         year = lubridate::year(dateofinci)) %>% 
  filter(year == 2008)

# spatial joining
ca_spills <- ca_counties %>% 
  st_join(spills_inland)

Obtain the number of 2008 spills by county

ca_spill_count <- ca_spills %>% 
  group_by(name) %>% 
  summarize(n_records = sum(!is.na(oesnumber)))

Create ggplot for visualization

ggplot(ca_spill_count, aes(fill = n_records))+
  geom_sf() +
  scale_fill_viridis_c(option = "A",direction = -1)+
  theme(plot.title = element_text(color = "#5b4f41", size = 16),
            plot.background = element_rect("white"),
            panel.background = element_rect("#faf7f2"),
            panel.grid = element_line(linetype= "longdash", color = "#f0ece1"),
            axis.text = element_text(color = "#5b4f41"),
            axis.title = element_text(color = "#5b4f41"),
            strip.background = element_rect("white"),
            axis.line = element_line(color = "#5b4f41"))+
  labs(x = "Latitude",
       y = "Longitude",
       fill = "# of Spills")+
  ggtitle("2008 California Oil Spills by County")
**Fig. 1** Map of California Counties colored according to the number of oil spills that occured in 2008. Lighter colors indicate fewer icidents than darker colors.

Fig. 1 Map of California Counties colored according to the number of oil spills that occured in 2008. Lighter colors indicate fewer icidents than darker colors.